home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
cexpert.zip
/
MCH7.LST
< prev
next >
Wrap
File List
|
1990-09-15
|
47KB
|
1,873 lines
Listing 7-1 The Inventory Control System Class Structure in C++
/*****************************************************************************
This header file defines the knowledge representation data structure
in inventory control system in C++.
Inventory Control System Class structure :
Basic_Class
|
---------------------------------------------------
| | | |
Location_Class Inventory_Class Customer_Class Sales_Records_Class
| | | |
---------- ------------- ------------ sf_sales_record_class
| | | | | |
Warehouse Dealer WH_Inv DL_Inv ST_Cus HY_Cus
| |
SF_Inv --------
| |
ST_Inv HY_Inv
*****************************************************************************/
/*
** icsclass.h
**
** Define each class.
**
** Date: 9/6/88
*/
/*---------------------------------------------------define class-----------*/
class basic_class {
friend class location;
friend class inv;
friend class cust;
friend class sales_records;
};
/*---------------------------------------------------cust class-------*/
class cust : public basic_class {
char_slot *dealer;
char_slot *name;
char_slot *part;
int_slot *number;
char_slot *order_date;è char_slot *time;
friend class st_cust;
friend class hayward_cust;
public:
cust();
~cust() { delete dealer; delete name; delete part;
delete order_date; delete number; delete time;
} /*destructor*/
void set_slotval(); /*set the slot value*/
Cust* get_slotval(); /*get the slot value*/
void prn_value();
};
/*------------------------------------------------st_cust class---*/
/*Inheritence from class cust
*/
class st_cust : public cust {};
/*---------------------------------------------------inv class-------*/
class inv : public basic_class {
char_slot *name; /*part name*/
int_slot *cil; /*current inv level*/
int_slot *oil; /*optimal inv level*/
friend class dealer_inv;
friend class warehs_inv;
public:
inv();
~inv() { delete name; delete cil; delete oil;}
void set_slotval();
Inv* get_slotval();
void prn_value();
};
/*--------------------------------------------dealer_inv class-----*/
/*Inherited from class inv
*/
class dealer_inv : public inv {};
/*------------------------------------------------st_inv class--------*/
/*Inherited from class dealer_inv
*/
class st_inv : public dealer_inv {};
/*--------------------------------------------warehs_inv class-----*/
/*Inherited from class inv
*/
class warehs_inv : public inv {
double_slot *cost;
double_slot *discount;è int_slot *min_order;
char_slot *order_time;
public:
warehs_inv();
~warehs_inv() { delete order_time; delete cost;
delete min_order; delete discount;}
void set_slotval1();
Ware_Inv* get_slotval1();
void prn_value1();
};
/*--------------------------------------------sf_inv class-----*/
/*Inheritence from class warehs_inv
*/
class sf_inv : public warehs_inv {};
/*-------------------------------------------------location class-----------*/
class location : public basic_class {
char_slot *address; /*address of the location*/
char_slot *phone; /*phone number of the loc*/
class invent : public inv {} *invntory; /*inventory subclass*/
friend class dealer;
friend class warehs;
public:
location();
~location() { delete address; delete phone;}
void set_slotval(); /*set the slot value*/
Loc* get_slotval1(); /*get the address, phone*/
Inv* get_slotval2(); /*get the subclass inventory value*/
void prn_value();
};
/*-------------------------------------------------dealer class-----------*/
/*Inherited from class location
*/
class dealer : public location {
class custmers : public cust {} *custm;
public:
dealer() { custm = new custmers;}
~dealer() { delete custm;}
void set_cust_slotval();
Cust* get_cust_slotval();
void prn_cust_slotval();
};
/*--------------------------------------------sales_records class-------*/
class sales_records : public basic_class {
int_slot *sale_number;
char_slot *date;
char_slot *part;
int_slot *number_sold;
double_slot *cost;
double_slot *total_sale;
char_slot *sold_to;
friend class sf_sales_records;
public:
sales_records();
~sales_records() { delete date; delete part;
delete sold_to; delete sale_number;
delete number_sold; delete cost;
delete total_sale;}
void set_slotval();
Sales_Rec* get_slotval();
void prn_value();
};
/*--------------------------------------------sf_sales_records class--*/
/*Inherited from class sales_records
*/
class sf_sales_records : public sales_records {};
/*-------------------------------------------------warehs class---------*/
/*Inherited from class location
*/
class warehs : public location {
class sales_recs : public sales_records {} *sales_rec;
public:
warehs() {sales_rec = new sales_recs;}
~warehs() { delete sales_rec;}
void set_sales_recs_slotval();
Sales_Rec* get_sales_recs_slotval();
};
/*
****************************************************************
**
** ics.h
**
** Define the overloaded functions which are used in building up KB.
** Date: 9/6/88
**
*/
#define FAIL 0
#define SUCCEED 1
struct slot_node { /*slot node used in slot list*/
char* name;
slot_node* next;
};
struct cls_slot {
char* name;
slot_node* sllist;
};
/*-----------------------------------------------------For ICS -------------*/
/*
** Define each class node and entity node structure for the leaves of class
** tree. Because C++ is compiler language instead of intepreter language.
** So it is impossible to dynamic define the data structure in the run time.
*/
/*
** st_cust class
*/
struct stcust_etynd { /*st_cust class entity node*/
char* name;
st_cust* entity;
stcust_etynd* next;
};
struct stcust_class { /*st_cust class node*/
char* name;
slot_node* slist;
stcust_etynd* elist;
};
/*
** st inv class
*/
struct stinv_etynd { /*st_inv class entity node*/
char* name;
st_inv* entity;
stinv_etynd* next;
};
struct stinv_class { /*st_inv class node*/
char* name;
slot_node* slist;
stinv_etynd* elist;
};
/*
** san francisco inv class
*/
struct sfinv_etynd { /*sf_inv class entity node*/
char* name;
sf_inv* entity;
sfinv_etynd* next;
};
struct sfinv_class { /*sf_inv class node*/
char* name;
slot_node* slist;
sfinv_etynd* elist;
};
/*
** warehouse class
*/
struct warehs_etynd {
char* name;
warehs* entity;
warehs_etynd* next;
};
struct warehs_class {
char* name;
slot_node* slist;
warehs_etynd* elist;
};
/*
** dealer class
*/
struct dealer_etynd {
char* name;
dealer* entity;
dealer_etynd* next;
};
struct dealer_class {
char* name;
slot_node* slist;
dealer_etynd* elist;
};
/*
** sf_sales_records class
*/
struct sfsalrec_etynd {
char* name;
sf_sales_records* entity;
sfsalrec_etynd* next;
};
struct sfsalrec_class {
char* name;
slot_node* slist;
sfsalrec_etynd* elist;
};
/*---------------------------------------------Overload define-----------*/
/*
** The best way to generalize the routines.
*/
overload addslot_clsnd;
void addslot_clsnd(cls_slot*,stcust_class*); /*for st_cust class*/
void addslot_clsnd(cls_slot*,stinv_class*); /*for st_inv class*/
void addslot_clsnd(cls_slot*,sfinv_class*); /*for sf_inv class*/
void addslot_clsnd(cls_slot*,warehs_class*); /*for warehs class*/
void addslot_clsnd(cls_slot*,dealer_class*); /*for dealer class*/
void addslot_clsnd(cls_slot*,sfsalrec_class*); /*for sf_sales_rec class*/
overload addety_clsnd;
void addety_clsnd(char*,st_cust*,stcust_class*); /*for st_cust class*/
void addety_clsnd(char*,st_inv*,stinv_class*); /*for st_inv class*/
void addety_clsnd(char*,sf_inv*,sfinv_class*); /*for sf_inv class*/
void addety_clsnd(char*,warehs*,warehs_class*); /*for warehs class*/
void addety_clsnd(char*,dealer*,dealer_class*); /*for dealer class*/
void addety_clsnd(char*,sf_sales_records*,sfsalrec_class*); /*for sf_sales_rec class*/
overload print_entity_name;
void print_entity_name(stcust_etynd*); /*for st_cust class*/
void print_entity_name(stinv_etynd*); /*for st_inv class*/
void print_entity_name(sfinv_etynd*); /*for sf_inv class*/
void print_entity_name(warehs_etynd*); /*for warehs class*/
void print_entity_name(dealer_etynd*); /*for dealer class*/
void print_entity_name(sfsalrec_etynd*); /*for sf_sales_rec class*/
overload print_clsnode;
void print_clsnode(stcust_class*); /*for st_cust class*/
void print_clsnode(stinv_class*); /*for st_inv class*/
void print_clsnode(sfinv_class*); /*for sf_inv class*/
void print_clsnode(warehs_class*); /*for warehs class*/
void print_clsnode(dealer_class*); /*for dealer class*/
void print_clsnode(sfsalrec_class*); /*for sf sale rec class*/
overload find_entity_node;
st_cust* find_entity_node(char*,stcust_class*); /*for st_cust class*/
st_inv* find_entity_node(char*,stinv_class*); /*for st_inv class*/
sf_inv* find_entity_node(char*,sfinv_class*); /*for sf_inv class*/
warehs* find_entity_node(char*,warehs_class*); /*for warehs class*/
dealer* find_entity_node(char*,dealer_class*); /*for dealer class*/
sf_sales_records* find_entity_node(char*,sfsalrec_class*); /*for sf sale class*/
overload find_all_entities;
stcust_etynd* find_all_entities(stcust_class*); /*for st_cust class*/
stinv_etynd* find_all_entities(stinv_class*); /*for st_inv class*/
sfinv_etynd* find_all_entities(sfinv_class*); /*for sf_inv class*/
warehs_etynd* find_all_entities(warehs_class*); /*for warehs class*/
dealer_etynd* find_all_entities(dealer_class*); /*for dealer class*/
sfsalrec_etynd* find_all_entities(sfsalrec_class*); /*for sf sal rec class*/
overload find_e_with_slot;
stcust_etynd* find_e_with_slot(char*,char*, stcust_class*); /*for st_cust class*/
stinv_etynd* find_e_with_slot(char*,char*, stinv_class*); /*for st_inv class*/
sfinv_etynd* find_e_with_slot(char*,char*,sfinv_class*); /*for sf_inv class*/
warehs_etynd* find_e_with_slot(char*,char*, warehs_class*); /*for warehs_inv class*/
dealer_etynd* find_e_with_slot(char*,char*,dealer_class*); /*for dealer_inv class*/
sfsalrec_etynd* find_e_with_slot(char*,char*,sfsalrec_class*); /*for sf sales records class*/
/*
***********************************************************
**
** slotdef.h
**
** Define the slot structure
** Date: 9/6/88
*/
struct char_slot { /*the slot value is char*/
char* slot_name;
char* slot_value;
};
struct double_slot { /*the slot value is double*/
char* slot_name;
double slot_value;
};
struct int_slot { /*the slot value is int*/
char* slot_name;
int slot_value;
};
#define SLOTNAME 30
#define SLOTVALUE 30
#define SLOT_END "END"
/*
******************************************************
**
** parse.h
**
** Define the structure used in parsing the query.
** Date: 9/6/88
**
*/
#define CASE1 1 /*C and E are instantiated without slot*/
#define CASE2 2 /*C is instantiated but E isn't without slot*/
#define CASE3 3 /*C and E are instantiated with slot var*/
#define CASE4 4 /*C is instantiated but E isn't with slot var*/
#define CASE5 5 /*C and E are instantiated with slot constant*/
#define CASE6 6 /*C is instantiated but E isn't with slot constant*/
#define CASE7 7 /*entity is instantiated and class isn't*/
#define ZERO 0
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
#define FIVE 5
#define SIX 6
#define SEVEN 7
#define EIGHT 8
/*
**
*********************************************************************
** tempstru.h
**
** This header file defines some structures for inputing and outputing
** the class slot data.
**
** Include: Location,Inventory,Customer
** Date: 7/22/88
*/
struct Loc { /*location*/
char_slot *address; /*address of the location*/
char_slot *phone; /*phone number of the loc*/
};
struct Inv { /*inventory*/
char_slot *name; /*part name*/
int_slot *cil; /*current inventory level*/
int_slot *oil; /*optimal inventory level*/
};
struct Cust { /*customer*/
char_slot *dealer;
char_slot *name;
char_slot *part;
int_slot *number;
char_slot *order_date;
char_slot *time;
};
struct Sales_Rec { /*sales records*/
int_slot *sale_number;
char_slot *date;
char_slot *part;
int_slot *number_sold;
double_slot *cost;
double_slot *total_sale;
char_slot *sold_to;
};
struct Ware_Inv { /*warehouse inventory*/
double_slot *cost;
double_slot *discount;
int_slot *min_order;
char_slot *order_time;
};
Listing 7-2 The ICS Class Constructors and Member Functions
/*
** icskb0.cpp
** Define the member functions of each class.
**
** Date: 9/6/88
*/
#include <stream.h>
#include <string.h>
#include <slotdef.h>
#include <tempstru.h>
#include <icsclass.h>
/*-------------------------------------------------------cust_class----*/
cust::cust()
{
dealer->slot_name = new char[SLOTNAME];
dealer->slot_name = "dealer";
dealer->slot_value = new char[SLOTVALUE];
*dealer->slot_value = NULL;
name->slot_name = new char[SLOTNAME];
name->slot_name = "name";
name->slot_value = new char[SLOTVALUE];
*name->slot_value = NULL;
part->slot_name = new char[SLOTNAME];
part->slot_name = "part";
part->slot_value = new char[SLOTVALUE];
*part->slot_value = NULL;
number->slot_name = new char[SLOTNAME];
number->slot_name = "number";
number->slot_value = 0;
order_date->slot_name = new char[SLOTNAME];
order_date->slot_name = "order_date";
order_date->slot_value = new char[SLOTVALUE];
*order_date->slot_value = NULL;
time->slot_name = new char[SLOTNAME];
time->slot_name = "time";
time->slot_value = new char[SLOTVALUE];
*time->slot_value = NULL;
}
Cust* cust::get_slotval() /*get the slot value*/
{
Cust* temp = new Cust;
temp->dealer->slot_value = dealer->slot_value;
temp->dealer->slot_name = dealer->slot_name;
temp->name->slot_value = name->slot_value;
temp->name->slot_name = name->slot_name;
temp->part->slot_value = part->slot_value;
temp->part->slot_name = part->slot_name;
temp->number->slot_value = number->slot_value;
temp->number->slot_name = number->slot_name;
temp->order_date->slot_value = order_date->slot_value;
temp->order_date->slot_name = order_date->slot_name;
temp->time->slot_value = time->slot_value;
temp->time->slot_name = time->slot_name;
return temp;
}
void cust::set_slotval() /*set the slot value*/
{
cout << "\n" << dealer->slot_name << ": ";
cin >> dealer->slot_value;
cout << name->slot_name << ": ";
cin >> name->slot_value;
cout << part->slot_name << ": ";
cin >> part->slot_value;
cout << number->slot_name << ": ";
cin >> number->slot_value;
cout << order_date->slot_name << ": ";
cin >> order_date->slot_value;
cout << time->slot_name << ": ";
cin >> time->slot_value;
}
void cust::prn_value()
{
cout << "\n" << dealer->slot_name << ": " << dealer->slot_value;
cout << " " << name->slot_name << ": " << name->slot_value;
cout << " " << part->slot_name << ": " << part->slot_value;
cout << " " << number->slot_name << ": " << number->slot_value;
cout << " " << order_date->slot_name << ": " << order_date->slot_value;
cout << " " << time->slot_name << ": " << time->slot_value;
}
/*-------------------------------------------------------inv_class----*/
inv::inv()
{
cil->slot_name = new char[SLOTNAME];
cil->slot_name = "cil";
cil->slot_value = oil->slot_value = 0;
oil->slot_name = new char[SLOTNAME];
oil->slot_name = "oil";
name->slot_name = new char[SLOTNAME];
name->slot_name = "name";
name->slot_value = new char[SLOTVALUE];
*name->slot_value = NULL;
è}
void inv::set_slotval()
{
cout << "\n" << name->slot_name << ": ";
cin >> name->slot_value;
cout << cil->slot_name << ": ";
cin >> cil->slot_value;
cout << oil->slot_name << ": ";
cin >> oil->slot_value;
}
Inv* inv::get_slotval()
{
Inv* temp = new Inv;
temp->name->slot_name = name->slot_name;
temp->name->slot_value = name->slot_value;
temp->cil->slot_name = cil->slot_name;
temp->cil->slot_value = cil->slot_value;
temp->oil->slot_name = oil->slot_name;
temp->oil->slot_value = oil->slot_value;
return temp;
}
void inv::prn_value()
{
cout << "\n" << name->slot_name << ": " << name->slot_value;
cout << " " << cil->slot_name << ": " << cil->slot_value;
cout << " " << oil->slot_name << ": " << oil->slot_value;
}
/*--------------------------------------------warehs_inv class-----*/
warehs_inv::warehs_inv()
{
cost->slot_name = new char[SLOTNAME];
cost->slot_name = "cost";
cost->slot_value = 0.0;
discount->slot_name = new char[SLOTNAME];
discount->slot_name = "discount";
discount->slot_value = 0.0;
min_order->slot_name = new char[SLOTNAME];
min_order->slot_name = "min_order";
min_order->slot_value = 0;
order_time->slot_name = new char[SLOTNAME];
order_time->slot_name = "order_time";
order_time->slot_value = new char[SLOTVALUE];
*order_time->slot_value = NULL;
}
è
void warehs_inv::set_slotval1()
{
cout << "\n" << cost->slot_name << ": ";
cin >> cost->slot_value;
cout << discount->slot_name << ": ";
cin >> discount->slot_value;
cout << min_order->slot_name << ": ";
cin >> min_order->slot_value;
cout << order_time->slot_name << ": ";
cin >> order_time->slot_value;
}
Ware_Inv* warehs_inv::get_slotval1()
{
Ware_Inv* temp = new Ware_Inv;
temp->cost->slot_name = cost->slot_name;
temp->cost->slot_value = cost->slot_value;
temp->discount->slot_name = discount->slot_name;
temp->discount->slot_value = discount->slot_value;
temp->min_order->slot_name = min_order->slot_name;
temp->min_order->slot_value = min_order->slot_value;
temp->order_time->slot_name = order_time->slot_name;
temp->order_time->slot_value = order_time->slot_value;
return temp;
}
void warehs_inv::prn_value1()
{
cout << "\n" << cost->slot_name << ": "<< cost->slot_value;
cout << " " << discount->slot_name << ": " << discount->slot_value;
cout << " " << min_order->slot_name << ": " << min_order->slot_value;
cout << " " << order_time->slot_name << ": " << order_time->slot_value;
}
/*-----------------------------------------------location_class-------------*/
location::location()
{
address->slot_name = new char[SLOTNAME];
address->slot_name = "address";
address->slot_value = new char[SLOTVALUE];
*address->slot_value = NULL;
phone->slot_name = new char[SLOTNAME];
phone->slot_name = "phone";
phone->slot_value = new char[SLOTVALUE];
*phone->slot_value = NULL;
invntory = new invent;
}
void location::set_slotval()
{
cout << "\n" << address->slot_name << ": ";
cin >> address->slot_value;è cout << phone->slot_name << ": ";
cin >> phone->slot_value;
invntory->set_slotval();
}
Loc* location::get_slotval1()
{
Loc* temp = new Loc;
temp->address->slot_name = address->slot_name;
temp->address->slot_value = address->slot_value;
temp->phone->slot_name = phone->slot_name;
temp->phone->slot_value = phone->slot_value;
return temp;
}
Inv* location::get_slotval2()
{
Inv* temp = new Inv;
temp = invntory->get_slotval();
return temp;
}
void location::prn_value()
{
cout << "\n" << address->slot_name << ": " << address->slot_value;
cout << " " << phone->slot_name << ": " << phone->slot_value;
invntory->prn_value(); /*print out the subclass inventory value*/
}
/*-------------------------------------------------------dealer_class-------*/
void dealer::set_cust_slotval()
{
custm->set_slotval();
}
Cust* dealer::get_cust_slotval()
{
Cust* temp = new Cust;
temp = custm->get_slotval();
return temp;
}
void dealer::prn_cust_slotval()
{
custm->prn_value();
}
/*-----------------------------------------------------warehs_class------*/è
void warehs::set_sales_recs_slotval()
{
sales_rec->set_slotval();
}
Sales_Rec* warehs::get_sales_recs_slotval()
{
Sales_Rec* temp = new Sales_Rec;
temp = sales_rec->get_slotval();
return temp;
}
/*--------------------------------------------sales_records class--*/
sales_records::sales_records()
{
sale_number->slot_name = new char[SLOTNAME];
sale_number->slot_name = "sale_number";
sale_number->slot_value = 0;
number_sold->slot_name = new char[SLOTNAME];
number_sold->slot_name = "number_sold";
number_sold->slot_value = 0;
date->slot_name = new char[SLOTNAME];
date->slot_name = "date";
date->slot_value = new char[SLOTVALUE];
*date->slot_value = NULL;
part->slot_name = new char[SLOTNAME];
part->slot_name = "part";
part->slot_value = new char[SLOTVALUE];
part->slot_value = NULL;
sold_to->slot_name = new char[SLOTNAME];
sold_to->slot_name = "sold_to";
sold_to->slot_value = new char[SLOTVALUE];
sold_to->slot_value = NULL;
cost->slot_name = new char[SLOTNAME];
cost->slot_name = "cost";
total_sale->slot_name = new char[SLOTVALUE];
total_sale->slot_name = "total_sale";
cost->slot_value = total_sale->slot_value = 0.0;
}
void sales_records::set_slotval()
{
cout << "\n" << sale_number->slot_name << ": ";
cin >> sale_number->slot_value;
cout << date->slot_name << ": ";
cin >> date->slot_value;
cout << part->slot_name << ": ";
cin >> part->slot_value;
cout << number_sold->slot_name << ": ";
cin >> number_sold->slot_value;
cout << cost->slot_name << ": ";è cin >> cost->slot_value;
cout << total_sale->slot_name << ": ";
cin >> total_sale->slot_value;
cout << sold_to->slot_name << ": ";
cin >> sold_to->slot_value;
}
Sales_Rec* sales_records::get_slotval()
{
Sales_Rec* temp = new Sales_Rec;
temp->sale_number->slot_name = sale_number->slot_name;
temp->sale_number->slot_value = sale_number->slot_value;
temp->date->slot_name = date->slot_name;
temp->date->slot_value = date->slot_value;
temp->part->slot_name = part->slot_name;
temp->part->slot_value = part->slot_value;
temp->number_sold->slot_name = number_sold->slot_name;
temp->number_sold->slot_value = number_sold->slot_value;
temp->cost->slot_name = cost->slot_name;
temp->cost->slot_value = cost->slot_value;
temp->total_sale->slot_name = total_sale->slot_name;
temp->total_sale->slot_value = total_sale->slot_value;
temp->sold_to->slot_name = sold_to->slot_name;
temp->sold_to->slot_value = sold_to->slot_value;
return temp;
}
void sales_records::prn_value()
{
cout << "\n" << sale_number->slot_name << ": " << sale_number->slot_value;
cout << " " << date->slot_name << ": " << date->slot_value;
cout << " " << part->slot_name << ": " << part->slot_value;
cout << " " << number_sold->slot_name << ": " << number_sold->slot_value;
cout << " " << cost->slot_name << ": " << cost->slot_value;
cout << " " << total_sale->slot_name << ": " << total_sale->slot_value;
cout << " " << sold_to->slot_name << ": " << sold_to->slot_value;
}
/*
***************************************************************
**
** Building up slot name lists and their initialization
**
** icskb1.cpp
** Define some funtions which will be used to do initialization.
** Date: 9/6/88
*/
#include <stream.h>
#include <string.h>
#include <slotdef.h>è#include <tempstru.h>
#include <icsclass.h>
#include <ics.h>
extern stcust_class* STCUST_CLASS; /*st_cust class node*/
extern stinv_class* STINV_CLASS; /*st_inv class node*/
extern sfinv_class* SFINV_CLASS; /*sf_inv class node*/
extern warehs_class* WAREHS_CLASS; /*warehs class node*/
extern dealer_class* DEALER_CLASS; /*dealer class node*/
extern sfsalrec_class* SFSALREC_CLASS; /*sf sale rec class node*/
/*
** Because C++ does not support the dynamic defining the data structure.
** In order to do the reasoning in an object oriented knowledge base, the
** developer should initialize each class name, the slot names which defined
** in the class definition before doing the reasoning.
*/
/*
** Make up the slot name list of the class
** Genaric function.
*/
cls_slot* make_up_cls_slot(char* clsname,char* slot[])
{
cls_slot* temp = new cls_slot;
int i = 0;
temp->name = clsname;
temp->sllist = NULL;
while(strcmp(slot[i],SLOT_END))
{
slot_node* temp1 = new slot_node;
temp1->name = slot[i];
temp1->next = temp->sllist;
temp->sllist = temp1;
i++;
}
return temp;
}
/*
** Insert slot name list into the class node.
** Overloaded function.
*/
/*for sf_inv class*/
void addslot_clsnd(cls_slot* c,stcust_class* s)
{
/* The following are same for different class. Overloaded!!*/
s->name = c->name;
s->slist = c->sllist;è}
/*for sf_inv class*/
void addslot_clsnd(cls_slot* c,sfinv_class* s)
{
/*...... same as above ....*/
}
/*for st_inv class*/
void addslot_clsnd(cls_slot* c,stinv_class* s)
{
/*...... same as above ....*/
}
/*for warehs class*/
void addslot_clsnd(cls_slot* c,warehs_class* s)
{
/*...... same as above ....*/
}
/*for dealer class*/
void addslot_clsnd(cls_slot* c,dealer_class* s)
{
/*...... same as above ....*/
}
/*for sf sale rec class*/
void addslot_clsnd(cls_slot* c,sfsalrec_class* s)
{
/*...... same as above ....*/
}
/*--------------------Below is not generic function------------------------*/
/*
** Initialize class node.
*/
void initstcustcls() /*for st_cust class*/
{
STCUST_CLASS = new stcust_class;
/* *STCUST_CLASS->name = NULL;*/
STCUST_CLASS->slist = NULL;
STCUST_CLASS->elist = NULL;
}
void initstinvcls() /*for st_inv class*/
{
STINV_CLASS = new stinv_class;
/* *STINV_CLASS->name = NULL;*/
STINV_CLASS->slist = NULL;
STINV_CLASS->elist = NULL;
}
void initsfinvcls() /*for sf_inv class*/
{
SFINV_CLASS = new sfinv_class;
*SFINV_CLASS->name = NULL;
SFINV_CLASS->slist = NULL;
SFINV_CLASS->elist = NULL;
}
void initwarehscls() /*for warehs class*/
{
WAREHS_CLASS = new warehs_class;
*WAREHS_CLASS->name = NULL;
WAREHS_CLASS->slist = NULL;
WAREHS_CLASS->elist = NULL;
}
void initdealercls() /*for dealer class*/
{
DEALER_CLASS = new dealer_class;
*DEALER_CLASS->name = NULL;
DEALER_CLASS->slist = NULL;
DEALER_CLASS->elist = NULL;
}
void initsfsalreccls() /*for sf sale rec class*/
{
SFSALREC_CLASS = new sfsalrec_class;
*SFSALREC_CLASS->name = NULL;
SFSALREC_CLASS->slist = NULL;
SFSALREC_CLASS->elist = NULL;
}
/*************************************************************
**
**
** Linking entity nodes into a tree structure for query
**
** icskb2.cpp
** Define some utility functions which are used to build up the KB.
** Date: 9/6/88
*/
#include <stream.h>
#include <string.h>
#include <slotdef.h>
#include <tempstru.h>è#include <icsclass.h>
#include <ics.h>
/*------------------------------------------Class Node--------------------*/
extern stcust_class* STCUST_CLASS; /*st_cust class node*/
extern stinv_class* STINV_CLASS; /*st_inv class node*/
extern sfinv_class* SFINV_CLASS; /*sf_inv class node*/
extern warehs_class* WAREHS_CLASS; /*warehs class node*/
extern dealer_class* DEALER_CLASS; /*dealer class node*/
extern sfsalrec_class* SFSALREC_CLASS; /*sf sale rec class node*/
/*
** Insert entity into the class node
** Overloaded function.
*/
/*For st cust class*/
void addety_clsnd(char* ename,st_cust* s,stcust_class* s1)
{
stcust_etynd* temp = new stcust_etynd;
/* The following are same for different class. Overloaded!!*/
temp->name = ename;
temp->entity = s;
temp->next = s1->elist;
s1->elist = temp;
}
/*For st inv class*/
void addety_clsnd(char* ename,st_inv* s,stinv_class* s1)
{
stinv_etynd* temp = new stinv_etynd;
/*...... same as above ....*/
}
/*For sf inv class*/
void addety_clsnd(char* ename,sf_inv* s,sfinv_class* s1)
{
sfinv_etynd* temp = new sfinv_etynd;
/*...... same as above ....*/
}
/*For warehs class*/
void addety_clsnd(char* ename,warehs* s,warehs_class* s1)
{
warehs_etynd* temp = new warehs_etynd;
/*...... same as above ....*/
}
/*For dealer class*/
void addety_clsnd(char* ename,dealer* s,dealer_class* s1)
{
dealer_etynd* temp = new dealer_etynd;
/*...... same as above ....*/
}
/*For dealer class*/
void addety_clsnd(char* ename,sf_sales_records* s,sfsalrec_class* s1)
{
sfsalrec_etynd* temp = new sfsalrec_etynd;
/*...... same as above ....*/
}
/*
** Print slot name
** Generic function.
*/
void print_slot_name(slot_node* c)
{
slot_node* temp;
temp = c;
cout << "\n" << "SLOT NAME : ";
while(temp != NULL)
{
cout << temp->name << " ";
temp = temp->next;
}
}
/*
** Print out entity.
** Overloaded function
*/
/*For st cust class*/
void print_entity_name(stcust_etynd* ety)
{
stcust_etynd* temp;
/* The following are same for different class. Overloaded!!*/
temp = ety;
cout << "\n" << "ENTITIES: ";
while(temp != NULL)
{
cout << "\n" << temp->name << ":";
temp->entity->prn_value();
temp = temp->next;
}
}
è/*For st inv class*/
void print_entity_name(stinv_etynd* ety)
{
stinv_etynd* temp;
/*...... same as above ....*/
}
/*For sf inv class*/
void print_entity_name(sfinv_etynd* ety)
{
sfinv_etynd* temp;
/*...... same as above ....*/
}
/*For dealer class*/
void print_entity_name(dealer* ety)
{
dealer* temp;
/*...... same as above ....*/
}
/*
** Print out class node.
** Overloaded function
*/
/*For st cust class*/
void print_clsnode(stcust_class* st)
{
/* The following are same for different class. Overloaded!!*/
cout << "\n" << "ClASS NAME: " << st->name;
print_slot_name(st->slist);
print_entity_name(st->elist);
}
/*For st inv class*/
void print_clsnode(stinv_class* st)
{
/*...... same as above ....*/
}
/*For sf inv class*/
void print_clsnode(sfinv_class* st)
{
/*...... same as above ....*/
}
/*For warehs class*/
void print_clsnode(warehs* st)
{
/*...... same as above ....*/}
/*For dealer class*/
void print_clsnode(dealer_class* st)
{
/*...... same as above ....*/
}
/*For sf sale rec class*/
void print_clsnode(sfsalrec_class* st)
{
/*...... same as above ....*/
}
Listing 7-3 Programs to Define Slots and Test Knowledge
Representation
/*
** icsmain.cpp
**
** This program is to implement the connction between Knowledge Base which
** is made up of objects and the query about the KB. There are some steps
** required before doing such kind of reasoning.
**
** The steps are:
** 1) define the class. e.g. structs.h.
** 2) define the kb. e.g. kbstruct.h
** 3) do the query.
**
** The query's format is:
** (entity ?x is-a ?y with slot_name = ?z)
**
** Date: 8/5/88
**
*/
#include <stream.h>
#include <string.h>
#include <slotdef.h>
#include <tempstru.h>
#include <icsclass.h>
#include <ics.h>
#include <parse.h>
#define MAXQUERY 60
/*
** Because C++ does not support the dynamic defining the data structure.
** In order to do the reasoning in an object oriented knowledge base, the
** developer should initialize each class name, the slot names which defined
** in the class definition before doing the reasoning.
*/
/*------------------------------------------Class Node--------------------*/
stcust_class* STCUST_CLASS; /*st_cust class node*/
stinv_class* STINV_CLASS; /*st_inv class node*/
sfinv_class* SFINV_CLASS; /*sf_inv class node*/
warehs_class* WAREHS_CLASS; /*warehs class node*/
dealer_class* DEALER_CLASS; /*dealer class node*/
sfsalrec_class* SFSALREC_CLASS; /*sf sale rec class node*/
/* The initialization for each class' slot as follow.
** the SLOT_END must be added in the end of each initialization.
*/
/*for cust class*/
char* cust_slot[7] = { "dealer", "name", "part", "number", "order_date",
"time", SLOT_END};
/*for inv class*/
char* inv_slot[4] = { "name", "cil", "oil", SLOT_END};
/*for warehs class*/
char* warehs_inv_slot[8] = { "name", "cil", "oil", "cost", "discount",
"min_order", "order_time", SLOT_END};
/*for location class*/
char* loc_slot[3] = { "address", "phone", SLOT_END};
/*for sales record class*/
char* salrec_slot[8] = { "sale_number", "date", "part", "number_sold", "cost",
"total_sale", "sold_to", SLOT_END};
/*-------------------------------------------------------------------------*/
main()
{
cls_slot* temp1;
extern void initstcustcls();
extern void initstinvcls();
extern void initsfinvcls();
extern cls_slot* make_up_cls_slot(char*,char*[]);
/*For st_cust*/
initstcustcls();
char* temp = new char[40];
temp = "st_cust";
temp1 = make_up_cls_slot(temp, cust_slot);
addslot_clsnd(temp1,STCUST_CLASS);
/*For entity creating and setting for st_cust*/
st_cust* janoschka_stephen = new st_cust;
char* te;
te = "janoschka_stephen";
addety_clsnd(te,janoschka_stephen,STCUST_CLASS);
st_cust* lisa_hu = new st_cust;
addety_clsnd("lisa_hu",lisa_hu,STCUST_CLASS);
cout << "\n" << "Enter Janoschka Stephen's Data, Please";
janoschka_stephen->set_slotval();
cout << "\n" << "Enter Lise Hu's Data, Please";
lisa_hu->set_slotval();
print_clsnode(STCUST_CLASS);
/*----------------------------------------------------do some queries------*/
char x[MAXQUERY];
cout << "\nEnter query: ";
gets(x);
int i;
extern int check_entity_pred_syntax(char*);
i = check_entity_pred_syntax(x);
extern char query_word[8][30];
char* class_name;
class_name = query_word[THREE];
switch (i)
{
case CASE1:
if(!strcmp(class_name,"st_cust"))
{
stcust_class* x;
st_cust* y;
x = STCUST_CLASS;
y = find_entity_node(query_word[ONE],x);
if ( y != NULL)
cout << "\nQuery is true!";
else
cout << "\nQuery is not true!";
}
if(!strcmp(class_name,"st_inv"))
{
stinv_class* x;
st_inv* y;
x = STINV_CLASS;
y = find_entity_node(query_word[ONE],x);
y->prn_value();
}
if(!strcmp(class_name,"sf_inv"))
{
sfinv_class* x;
sf_inv* y;
x = SFINV_CLASS;
y = find_entity_node(query_word[ONE],x);
y->prn_value();
}
if(!strcmp(class_name,"warehs"))
{
warehs_class* x;
warehs* y;
x = WAREHS_CLASS;
y = find_entity_node(query_word[ONE],x);
y->prn_value();
}
if(!strcmp(class_name,"dealer"))
{
dealer_class* x;
dealer* y;
x = DEALER_CLASS;
y = find_entity_node(query_word[ONE],x);
y->prn_value();
}
if(!strcmp(class_name,"sfsalrec"))
{
sfsalrec_class* x;
sf_sales_records* y;
x = SFSALREC_CLASS;
y = find_entity_node(query_word[ONE],x);
y->prn_value();
}
break;
case CASE2:
if(!strcmp(class_name,"st_cust"))
{
stcust_class* x;
stcust_etynd* y;
x = STCUST_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
if(!strcmp(class_name,"st_inv"))
{
stinv_class* x;
stinv_etynd* y;
x = STINV_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
if(!strcmp(class_name,"sf_inv"))
{
sfinv_class* x;
sfinv_etynd* y;
x = SFINV_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
if(!strcmp(class_name,"warehs"))
{
warehs_class* x;
warehs_etynd* y;
x = WAREHS_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
if(!strcmp(class_name,"dealer"))
{
dealer_class* x;
dealer_etynd* y;
x = DEALER_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
if(!strcmp(class_name,"sfsalrec"))
{
sfsalrec_class* x;
sfsalrec_etynd* y;
x = SFSALREC_CLASS;
y = find_all_entities(x);
print_entity_name(y);
}
break;
case CASE6:
if(!strcmp(class_name,"st_cust"))
{
stcust_class* x;
stcust_etynd* y;
x = STCUST_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
if(!strcmp(class_name,"st_inv"))
{
stinv_class* x;
stinv_etynd* y;
x = STINV_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
if(!strcmp(class_name,"sf_inv"))
{
sfinv_class* x;
sfinv_etynd* y;
x = SFINV_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
if(!strcmp(class_name,"warehs"))
{
warehs_class* x;
warehs_etynd* y;
x = WAREHS_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
if(!strcmp(class_name,"dealer"))
{
dealer_class* x;
dealer_etynd* y;
x = DEALER_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
if(!strcmp(class_name,"sfsalrec"))
{
sfsalrec_class* x;
sfsalrec_etynd* y;
x = SFSALREC_CLASS;
char* s1;
char* s2;
s1 = query_word[FIVE];
s2 = query_word[SEVEN];
y = find_e_with_slot(s1,s2,x);
print_entity_name(y);
}
break;
}
}
Listing 7-4 Sample Program for the Rule Structure
/*
** Rule.h
*/
typedef struct _rule {
char *name; /* the name of the rule */
cons *premise; /* the premise predicates */
cons *conclusion; /* the conclusion predicate */
double certainty; /* the certainty of this rule */
struct _rule *next; /* the next rule in the database */
} rule;
extern rule *make_rule();
extern rule *delete_rule();
extern rule *find_rule();
extern void add_rule();
extern void kill_rule();
extern void print_rule();
extern void printall_rules();
extern rule *RuleDatabase; /* pointer to all rules */
/*
*******************************************************************
**
** Rule.c
*/
/*
**
** (new format of rules)
*/
#include <math.h>
#include <stdio.h>
#include "cons.h"
#include "rule.h"
rule *RuleDatabase = NULL; /* pointer to all rules */
/*
** read_rule(): reads the rule from the standard input. Rules look like
** this:
** Rule-name IF (pred*) THEN pred
** E.g:
** Fuel-rule-1 IF ((fuel-level low)) THEN (out-of-fuel)
*/
rule *read_rule()
{ rule *rp;
cons *pp;
if (rp = (rule *) malloc(sizeof (rule))) {
pp = lread(C_FILE,stdin); /* first the name */
rp->name = pp->car.s; /* save the name */
free(pp); /* junk the cons */
pp = lread(C_FILE,stdin); /* get the 'IF' */
if (strcmpi(pp->car.s,"IF")) { /* not if? */
free(rp); /* not a rule! */
killcons(pp);
return NULL;
}
killcons(pp); /* get rid of it */
rp->premise = lread(C_FILE,stdin);
pp = lread(C_FILE,stdin); /* get the 'THEN' */
if (strcmpi(pp->car.s,"THEN")) { /* not then? */
killcons(rp->premise); /* not a rule! */
killcons(pp);
free(rp);
return NULL;
}
killcons(pp); /* get rid of 'then' */
rp->conclusion = lread(C_FILE,stdin);
pp = lread(C_FILE,stdin); /* lastly, the certainty */
rp->certainty = atof(pp->car.s);
killcons(pp); /* junk the certainty */
rp->next = NULL;
}
return rp;
}
/*
** _add_rule(): adds the rule _rp_ to the database _dbp_
*/
static rule *_add_rule(rp,dbp)
rule *rp,*dbp;
{
rule *rp2 = dbp;
if (dbp == NULL) {
return rp;
}
while (rp2->next) {
rp2 = rp2->next;
}
rp2->next = rp;
return dbp;
}
/*
** add_rule(): adds the rule _rp_ to the RuleDatabase
*/
void add_rule(rp)
rule *rp;
{
RuleDatabase = _add_rule(rp,RuleDatabase);
}
rule *delete_rule(rp,dbp)
rule *rp,*dbp;
{
rule *rrp;
if (dbp == NULL) {
return NULL;
} else if (rp == dbp) {
rrp = dbp->next;
kill_rule(rp);
return dbp->next;
} else {
dbp->next = delete_rule(rp,dbp->next);
return dbp;
}
}
/*
** find_rule: find rule by name
*/
rule *find_rule(s,dbp)
char *s;
rule *dbp;
{
if (dbp == NULL) {
return NULL;
} else if (!strcmp(s,dbp->name)) {
return(dbp);
} else {
return find_rule(s,dbp->next);
}
}
void kill_rule(rp)
rule *rp;
{
killcons(rp->premise);
killcons(rp->conclusion);
free(rp);
}
void print_rule(rp)
rule *rp;
{
printf("Name: %s\nPremise: ",rp->name);
lprint(rp->premise,C_FILE,stdout);
printf("\nConclusion: ");
lprint(rp->conclusion,C_FILE,stdout);è printf("\nCertainty: %g\n",rp->certainty);
}
void printall_rules(rule_base)
rule *rule_base;
{
rule *db = rule_base;
while (db != NULL) {
print_rule(db);
db = db->next;
}
}
Listing 7-5 Program for Logic Structure (Unstructured
Fact Base)
/*
** fact.h
*/
typedef struct _fact {
struct _fact *next;
cons *predicate;
double cert;
} fact;
extern fact *PkbList;
/*
*******************************************************************
**
** Facts.c
**
**
** facts are stored as predicate-certainty pairs.
** stash(): add a fact to the data base
**
*/
/*-------------------------------------------------------include------------*/
#include <stdio.h>
#include "cons.h"
#include <math.h>
#include "fact.h"
#include "goal.h"
/*-------------------------------------------------------global variables---*/
fact *PkbList = NULL;
/*-------------------------------------------------------read_fact----------*/
fact *read_fact()
{
fact *fp;
cons *pp;
if(fp = (fact *)malloc(sizeof(fact)))
{
fp->predicate = lread(C_FILE,stdin);
pp = lread(C_FILE,stdin);
fp->cert = atof(pp->car.s);
killcons(pp);
fp->next = NULL;è }
return fp;
}
/*-------------------------------------------------------_pkb_stash()-------*/
static fact *_pkb_stash(fp,fbp)
fact *fp,*fbp;
{
fact *fp2 = fbp;
if (fbp == NULL) {
return fp;
}
while (fp2->next) {
fp2 = fp2->next;
}
fp2->next = fp;
return fbp;
}
/*-------------------------------------------------------pkb_stash()-------*/
void pkb_stash(predicate,cert)
cons *predicate;
double cert;
{
fact *fp = (fact *) malloc(sizeof(fact));
if (fp) {
fp->predicate = predicate;
fp->cert = cert;
fp->next = NULL;
PkbList = _pkb_stash(fp,PkbList);
}
}
/*------------------------------------------------------print_fact()-------*/
void print_fact(fp)
fact *fp;
{
printf("Predicate: "); lprint(fp->predicate,C_FILE,stdout);
printf("\nCertainty: %lg\n\n",fp->cert);
}
/*------------------------------------------------------print_facts()-------*/
void printall_facts() {
fact *fp = PkbList;
while (fp != NULL) {
print_fact(fp);
fp = fp->next;
}
}
/*-------------------------------------------------lookup_pkb_fact()-------*/
/*
** lookup_pkb_fact(): Look up the pkb.
** Return struct Ret_Pair (substitution,certainty).
** Author: Sony Y. Song
** Date: 7/8/88
*/
Ret_Pair *lookup_pkb_fact(pattern,prev_subst)
cons *pattern;
cons *prev_subst;
{
Ret_Pair *ret_pair;
fact *fp = PkbList;
cons *subst;
subst = NULL;
if (ret_pair = (Ret_Pair *) malloc(sizeof(Ret_Pair)))
{
ret_pair->subst = NULL;
ret_pair->certainty = 0.0;
}
else
{
puts("\n*** Yow! Out of core ***\n");
}
while (fp != NULL) {
subst = unify_pred_c(pattern,fp->predicate);
if (subst != NULL && !test_subst_used(subst,prev_subst))
{
ret_pair->subst = subst;
ret_pair->certainty = fp->cert;
break;
} else {
fp = fp->next;
}
}
killcons(subst);
return ret_pair;
}
/*-------------------------------------------------delete_PKB()-------*/
void delete_PKB()
{
fact *PkbList = NULL;
}